import { Viewer, ScreenSpaceEventHandler } from 'cesium'

export default class PlanPath {
  private viewer: Viewer
  private _handler: ScreenSpaceEventHandler

  constructor(viewer: Viewer) {
    this.viewer = viewer
    this._handler = new ScreenSpaceEventHandler(this.viewer.scene.canvas)
  }

  /**
   * 创建鼠标左键事件
   */
  private _creatMouseLeft() {
    this._handler.setInputAction((event) => {
      const coordinates: [number, number, number] | null = c3toCoordinates(
        c2toC3(event.position, this.viewer),
        this.viewer
      )
      if (coordinates) {
        this.addPositions(coordinates)
        this.addLabel(coordinates)
        this.createPoint(coordinates)
        if (!this.line) {
          this.line = this.createLine()
        }
      }
    }, ScreenSpaceEventType.LEFT_CLICK)
  }

  /**
   * 创建鼠标右键事件
   */
  private _creatMouseRight() {
    this._handler.setInputAction((event) => {
      const position: [number, number, number] | null = c3toCoordinates(
        c2toC3(event.position, this.viewer),
        this.viewer
      )
      if (position) {
        this.currentPositions[this.currentPositions.length - 1] = position
        this.createPoint(position)
        this.addLabel(position)
        this.addDeleteBillboard()
        this.addRubbishBillboard(position)
        this.destroyMouse()
        this.initData()
        this.option?.completeCallback?.()
      }
    }, ScreenSpaceEventType.RIGHT_CLICK)
  }

  /**
   * 创建鼠标移动事件
   */
  private _creatMouseMove() {
    this._handler.setInputAction((event) => {
      const coordinates: [number, number, number] | null = c3toCoordinates(
        c2toC3(event.endPosition, this.viewer),
        this.viewer
      )
      if (this.currentPositions.length === 0) return
      if (coordinates) {
        this.currentPositions[this.currentPositions.length - 1] = coordinates
      }
      // 添加鼠标移动的label
      if (!this.moveLabel) {
        this.moveLabel = this.addMouseMoveLabel()
      }
    }, ScreenSpaceEventType.MOUSE_MOVE)
  }
}
